package com.jie.serviceedu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jie.serviceedu.entity.EduTeacher;
import com.jie.serviceedu.entity.vo.TeacherQuery;
import com.jie.serviceedu.service.EduTeacherService;
import com.jie.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.val;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author gaoshuo
 * @since 2021-05-13
 */
@RestController
@Api(description="讲师管理")
@CrossOrigin //跨域
@RequestMapping("/serviceedu/edu-teacher")
public class EduTeacherController {
        @Autowired
    private EduTeacherService eduTeacherService;

    @ApiOperation(value = "所有讲师列表")
        @GetMapping("findAll")
    public R findAllTeacher(){
            List<EduTeacher> list = eduTeacherService.list(null);
            return R.ok().data("lists",list);
        }

        @ApiOperation(value = "逻辑删除")
        @DeleteMapping("{id}")
    public R removeTeacher( @ApiParam(name = "id", value = "讲师ID", required = true) @PathVariable String id){
            boolean b = eduTeacherService.removeById(id);
            if (b){
                return R.ok();
            }else {
                return R.error();
            }
        }
    @ApiOperation(value = "分页讲师列表")
    @GetMapping("{page}/{limit}")
    public R pageList(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit){

        Page<EduTeacher> pageParam = new Page<>(page, limit);

        eduTeacherService.page(pageParam, null);
        List<EduTeacher> records = pageParam.getRecords();
        long total = pageParam.getTotal();

        return  R.ok().data("total", total).data("rows", records);
    }
    @ApiOperation(value = "过滤查询")
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition( @PathVariable Long current,
                                   @PathVariable Long limit,
                                   @RequestBody(required = false) TeacherQuery teacherQuery
    ){
        Page<EduTeacher> pageParam = new Page<>(current,limit);
        QueryWrapper wrapper = new QueryWrapper<>();

        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();

        if (!StringUtils.isEmpty(name)) {
            wrapper.like("name", name);
        }

        if (!StringUtils.isEmpty(level) ) {
            wrapper.eq("level", level);
        }

        if (!StringUtils.isEmpty(begin)) {
            wrapper.ge("gmt_create", begin);
        }

        if (!StringUtils.isEmpty(end)) {
            wrapper.le("gmt_create", end);
        }


        eduTeacherService.page(pageParam,wrapper);
        long total = pageParam.getTotal();
        List<EduTeacher> records = pageParam.getRecords();
        return  R.ok().data("total", total).data("rows", records);
    }

    /**
     * 添加教师方法
     */
    @ApiOperation(value = "新增讲师")
    @PostMapping("addTeacher")
    public R save(
            @ApiParam(name = "teacher", value = "讲师对象", required = true)
            @RequestBody EduTeacher teacher){

        eduTeacherService.save(teacher);
        return R.ok();
    }
    /**
     * 根据id查询
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping("getId/{id}")
        public R getId(@PathVariable String id){
        EduTeacher byId = eduTeacherService.getById(id);
       return R.ok().data("id",byId);
    }
    @ApiOperation(value = "根据id修改")
    @PostMapping("updateId")
    public R updateId(@RequestBody EduTeacher teacher){
        boolean id = eduTeacherService.updateById(teacher);
        if (id){
           return R.ok();
        }else {
            return R.error();
        }
    }
    }

